package foundation.pcmUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import org.jdom2.Element;
import org.jdom2.JDOMException;

import foundation.fileUtil.XMLFileUtil;

public class ResEnvirModelHelper {
	private XMLFileUtil xmlHelper = null;
	private String resEnvirFileName = null;

	public ResEnvirModelHelper() {

	}

	public ResEnvirModelHelper(String resEnvirFileName) {
		this.resEnvirFileName = resEnvirFileName;
		File resEnvirFile = new File(resEnvirFileName);
		xmlHelper = new XMLFileUtil(resEnvirFile);
	}

	public List<Element> readNode(String pathName) throws JDOMException {
		return xmlHelper.readNode(pathName);
	}

	public List<Element> readNode(String pathName, String prefix, String url) throws JDOMException {
		return xmlHelper.readNode(pathName, prefix, url);
	}

	public synchronized void write() throws FileNotFoundException, IOException {
		xmlHelper.write(resEnvirFileName);
	}

	public void close() {
		xmlHelper.close(resEnvirFileName);
	}
	
	/**
	 * 更改某个server的CPU处理速度
	 * @param entityName
	 * @param spd
	 */
	public void mdfyCPUSpd(String entityName,float spd){
		String path = "/resourceenvironment:ResourceEnvironment/"
				+ "resourceContainer_ResourceEnvironment[@entityName='" + entityName + "']/"
				+ "activeResourceSpecifications_ResourceContainer";

		Element procRate;
		try {
			procRate = xmlHelper.readNode(path, "resourceenvironment",
					"http://palladiosimulator.org/PalladioComponentModel/ResourceEnvironment/5.1").get(0);
			Element cpuProcRate = procRate.getChildren().get(2);
			cpuProcRate.setAttribute("specification", String.valueOf(spd));
			// 将要更改的数据写到更改后文件
			write();
		} catch (JDOMException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
			
	}
	
	/**
	 * 更改某个server的HDD处理速度
	 * @param entityName
	 * @param spd
	 */
	public void mdfyHDDSpd(String entityName,float spd){
		
		String path = "/resourceenvironment:ResourceEnvironment/"
				+ "resourceContainer_ResourceEnvironment[@entityName='" + entityName + "']/"
				+ "activeResourceSpecifications_ResourceContainer";
		try {
			Element procRate = xmlHelper.readNode(path, "resourceenvironment",
					"http://palladiosimulator.org/PalladioComponentModel/ResourceEnvironment/5.1").get(1);
			Element hddprocRate = procRate.getChildren().get(2);
			hddprocRate.setAttribute("specification", String.valueOf(spd));
			// 将要更改的数据写到更改后文件
			write();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		}
	}
	
	public String getHDDSpdBySvrname(String entityName){
		String spd = null;
		String path = "/resourceenvironment:ResourceEnvironment/"
				+ "resourceContainer_ResourceEnvironment[@entityName='" + entityName + "']/"
				+ "activeResourceSpecifications_ResourceContainer";
		try {
			Element procRate = xmlHelper.readNode(path, "resourceenvironment",
					"http://palladiosimulator.org/PalladioComponentModel/ResourceEnvironment/5.1").get(1);
			Element hddProcRate = procRate.getChildren().get(2);
			spd = hddProcRate.getAttributeValue("specification");
		} catch (JDOMException e) {
			e.printStackTrace();
		}
		return spd;
	}

	/**
	 * 在resourceenvironment模型中，根据entityName获取server的ID
	 * 
	 * @param entityName
	 * @return
	 */
	public String getServerIDByName(String entityName) {
		String serverID = null;
		String path = "/resourceenvironment:ResourceEnvironment/"
				+ "resourceContainer_ResourceEnvironment[@entityName='" + entityName + "']";
		Element serverIDEle;
		try {
			List<Element> serverIDs= xmlHelper.readNode(path, "resourceenvironment",
					"http://palladiosimulator.org/PalladioComponentModel/ResourceEnvironment/5.1");
			serverIDEle =serverIDs.get(0);
			serverID = serverIDEle.getAttributeValue("id");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return serverID;
	}

	public String getServerNameByID(String svrID) {
		String serverName = null;
		String path = "/resourceenvironment:ResourceEnvironment/" + "resourceContainer_ResourceEnvironment[@id='"
				+ svrID + "']";
		Element serverNameEle;
		try {
			serverNameEle = xmlHelper.readNode(path, "resourceenvironment",
					"http://palladiosimulator.org/PalladioComponentModel/ResourceEnvironment/5.1").get(0);
			serverName = serverNameEle.getAttributeValue("entityName");
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return serverName;
	}
	
	public String getCPUSpdBySvrname(String entityName){
		String spd = null;
		String path = "/resourceenvironment:ResourceEnvironment/"
				+ "resourceContainer_ResourceEnvironment[@entityName='" + entityName + "']/"
				+ "activeResourceSpecifications_ResourceContainer";
		try {
			Element procRate = xmlHelper.readNode(path, "resourceenvironment",
					"http://palladiosimulator.org/PalladioComponentModel/ResourceEnvironment/5.1").get(0);
			Element cpuProcRate = procRate.getChildren().get(2);
			spd = cpuProcRate.getAttributeValue("specification");
		} catch (JDOMException e) {
			e.printStackTrace();
		}
		return spd;
	}

	public XMLFileUtil getXmlHelper() {
		return xmlHelper;
	}

	public void setXmlHelper(XMLFileUtil xmlHelper) {
		this.xmlHelper = xmlHelper;
	}

	public String getResEnvirFileName() {
		return resEnvirFileName;
	}

	public void setResEnvirFileName(String resEnvirFileName) {
		this.resEnvirFileName = resEnvirFileName;
	}

}
